package com.cybertron.lockdown.spider.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * Created by xiezhouyan on 15-1-13.
 */
public class ConnectionUtil {

    private static final ComboPooledDataSource DATA_SOURCE = new ComboPooledDataSource();

    private static Logger logger = LoggerFactory.getLogger(ConnectionUtil.class);


    static {
        try {
            DATA_SOURCE.setUser(PropertiesUtil.get("db.username"));
            DATA_SOURCE.setPassword(PropertiesUtil.get("db.password"));
            DATA_SOURCE.setJdbcUrl(PropertiesUtil.get("db.url"));
            DATA_SOURCE.setDriverClass("com.mysql.jdbc.Driver");
            DATA_SOURCE.setInitialPoolSize(2);
            DATA_SOURCE.setMinPoolSize(1);
            DATA_SOURCE.setMaxPoolSize(10);
            DATA_SOURCE.setMaxStatements(50);
            DATA_SOURCE.setMaxIdleTime(60);
        } catch (PropertyVetoException e) {
            logger.error("init dataSource error", e);
        }
    }

    public static DataSource getDataSource() {
        return DATA_SOURCE;
    }

}
